package com.lukebang.util;

import java.util.HashMap;
import java.util.Map;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.lukebang.dao.CityCostRuleDAO;
import com.lukebang.pojo.CityCostRule;
import com.lukebang.pojo.Good;
import com.lukebang.util.ClothCurtain.Arith;

/**
 * 计算单个goods的价格
 * 
 * @author xiaGuanghui 2017年6月12日 下午3:24:03
 *
 */
public class CalculateOneGoodsPrice {

	private static Logger logger = LogManager.getLogger(CalculateOneGoodsPrice.class.getName());

	private static CityCostRuleDAO cityCostRuleDAO;

	public static void setCityCostRuleDAO(CityCostRuleDAO cityCostRuleDAO) {
		CalculateOneGoodsPrice.cityCostRuleDAO = cityCostRuleDAO;
	}

	private static String cityCodeStr;

	/**
	 * 计算单个goods的价格静态方法 xiaGuanghui 2017年6月12日 下午3:26:43
	 * 
	 * @param good
	 *            传来的goods对象
	 * @param finishShade
	 *            goods下面的成品帘
	 * @param clothShade
	 *            goods下面的布艺帘
	 * @param yarnShade
	 *            goods下面的纱帘
	 * @param headShade
	 *            headShade下面的帘头
	 * @param cityCodeStr
	 *            goods所属城市,根据查找该城市的收费标准
	 * @return
	 */
	public Double calculateOneGoods(CityCostRuleDAO cityCostRuleDAO, Good good, String finishShade,
			String clothShade, String yarnShade, String headShade, String cityCodeStr, Integer type) {
		CalculateOneGoodsPrice.cityCodeStr = cityCodeStr;
		CalculateOneGoodsPrice.cityCostRuleDAO = cityCostRuleDAO;
		// 该goods的价格值
		Double indentPrice = 0.00;
		// goods的type 1成品帘0 布艺帘
		Integer  goodsType=null;
		if(good !=null){
			goodsType = good.getType();
		}
		/**
		 * 如果该goods是布艺帘,则finishShade=null.
		 * 如果该goods是成品帘,则clothShade=null,yarnShade=null,headShade=null.
		 */
		return goodsType == 0 ? calculateFabric(indentPrice, good, clothShade, yarnShade, headShade, type)
				: (goodsType == 1 ?calculateFinished(indentPrice, good, finishShade, type) :0.0);
	}

	/**
	 * 计算goods的成品帘价格 xiaGuanghui 2017年6月12日 下午4:21:39
	 * 
	 * @param good
	 *            goods对象
	 * @param finishShade
	 *            goods旗下的成品帘
	 * @return
	 */
	public Double calculateFinished(Double indentPrice, Good good, String finishShade,Integer type) {
		Map<String, Object> costParam = new HashMap<>();
		costParam.put("type", "成品帘");
		costParam.put("cityCode", cityCodeStr);
		costParam.put("territoryType", type);
		CityCostRule cityCostRule = cityCostRuleDAO.selectAPrice(costParam);
		if (cityCostRule==null) {
			return indentPrice;
		}
		JSONArray finishArray = JSONArray.parseArray(finishShade);
		// 基础宽
		double widthBasisMeter = cityCostRule.getWidthBasisMeter();
		// 基础高
		double heightBasisMeter = cityCostRule.getHeightBasisMeter();
		
		for (int index = 0; index < finishArray.size(); index++) {
			JSONObject jsonObject = finishArray.getJSONObject(index);
			System.err.println("成品帘安装费计算jsonObject===>>"+jsonObject);
			double height = jsonObject.getDoubleValue("high");
			double width = jsonObject.getDoubleValue("wide");
			
			
			if (widthBasisMeter >= width && heightBasisMeter >= height) {// 宽高均在正常范围内,isSingle控制计算单双层价格
				indentPrice = Arith.add(indentPrice, cityCostRule.getSingle());
			} else if (widthBasisMeter >= width && heightBasisMeter < height) {// 宽正常，高超标
				// 计算工人价格(订单价)
				indentPrice = pricePartOne(cityCostRule, height, heightBasisMeter, indentPrice);

			} else if (widthBasisMeter < width && heightBasisMeter >= height) {// 宽超标，高正常
				// 计算工人价格(订单价)
				indentPrice = pricePartTwo(cityCostRule, width, widthBasisMeter, indentPrice);
			} else {// 宽超标，高超标
				// 计算工人价格(订单价)
				indentPrice = pricePartThree(cityCostRule, width, height, widthBasisMeter, heightBasisMeter, indentPrice);
			}
			
//			if (height > heightBasisMeter) {
//				Double reduction = Arith.sub(height, heightBasisMeter);// 高度超标量
//				Double chaochu = Arith.mul((Math.ceil(Arith.div(reduction, cityCostRule.getHeightBeyondMeter()))),
//						cityCostRule.getHeightBeyondUnitPrice());
//				indentPrice = Arith.add(Arith.add(basePrice, chaochu), indentPrice);
//			} else {
//				indentPrice = indentPrice + basePrice;
//			}

		}
		return indentPrice;
	}
	
	// 计算高超标价格
	private double pricePartOne(CityCostRule part, Double height, Double heightBasisMeter, Double price) {
		double basePrice = part.getSingle();// 基础价格，便于宽度或高度超标时累计单价
		Double reduction = Arith.sub(height, heightBasisMeter);// 高度超标量
		Double chaochu = Arith.mul((Math.ceil(Arith.div(reduction, part.getHeightBeyondMeter()))),
				part.getHeightBeyondUnitPrice());
		price = Arith.add(Arith.add(basePrice, chaochu), price);
		// return Math.max(price, part.getInstallStartingFare());
		return price;
	}

	// 计算宽超标价格
	private double pricePartTwo(CityCostRule part, Double width, Double widthBasisMeter, Double price) {
		// 计算商家价格
		double basePrice = part.getSingle();// 基础价格，便于宽度或高度超标时累计单价
		Double reduction = Arith.sub(width, widthBasisMeter);// 宽度超标量
		Double chaochu = Arith.mul((Math.ceil(Arith.div(reduction, part.getWidthBeyondMeter()))), basePrice);
		price = Arith.add(Arith.add(basePrice, chaochu), price);
		// return Math.max(price, part.getInstallStartingFare());
		return price;
	}

	// 计算宽高都超标价格
	private double pricePartThree(CityCostRule part, Double width, Double height, Double widthBasisMeter,
			Double heightBasisMeter, Double price) {
		// 计算商家价格
		double basePrice = part.getSingle();// 基础价格，便于宽度或高度超标时累计单价
		Double reductions = Arith.sub(height, heightBasisMeter);// 高度超标量 5
		Double reduction = Arith.sub(width, widthBasisMeter);// 宽度超标量 6
		Double chaochus = Arith.mul((Math.ceil(Arith.div(reductions, part.getHeightBeyondMeter()))),
				part.getHeightBeyondUnitPrice());// 100
		Double heightPrice = Arith.add(basePrice, chaochus);// 120
		Double total = Arith.mul((Math.ceil(Arith.div(reduction, part.getWidthBeyondMeter()))) + 1, heightPrice);// 360
		price = Arith.add(total, price);
		// return Math.max(price, part.getInstallStartingFare());
		return price;
	}

	/**
	 * 计算goods的布艺帘的价格,布艺帘的根据structure可以分为单布,单纱,布纱双层
	 * structure=0,structure=1,structure=2 xiaGuanghui 2017年6月12日 下午4:22:42
	 * 
	 * @param good
	 *            goods对象
	 * @param clothShade
	 *            goods下的布艺帘布尺寸
	 * @param yarnShade
	 *            goods下的纱尺寸
	 * @param headShade
	 *            goods下的帘头尺寸
	 * @return
	 */
	public static Double calculateFabric(Double indentPrice, Good good, String clothShade, String yarnShade,
			String headShade, Integer type) {
		int structure = good.getStructure();
		return structure == 0 ? calculateFabricCloth(indentPrice, good, clothShade, headShade, type)
				: calculateFabricYarnOrClothAndYarn(indentPrice, good, clothShade, yarnShade, headShade, type);
	}

	/**
	 * 单布 每一个单布尺寸包含数据库中goods_special和goods_special的accId对应的goods的值 xiaGuanghui
	 * 2017年6月12日 下午4:35:46
	 * 
	 * @param good
	 * @param clothShade
	 * @param headShade
	 * @return
	 */
	private static Double calculateFabricCloth(Double indentPrice, Good good, String clothShade, String headShade,
			Integer type) {
		JSONArray clothArray = JSONArray.parseArray(clothShade);
		if(clothArray.size()>0){
			for (int index = 0; index < clothArray.size(); index++) {
				JSONObject clothObject = (JSONObject) clothArray.get(index);
				/**
				 * 计算goods_special的accId对应的goods的价格
				 * 需要goods的accessoriesType的值,goods所属城市 goods的saleNumber,high
				 */
				System.out.println("-----1---------------------");
				// 准备查找城市收费的标准的参数
				Map<String, Object> costParam = new HashMap<>();
				if(clothObject != null){
					costParam.put("type", clothObject.getString("accessoriesType"));
					costParam.put("cityCode", cityCodeStr);
					indentPrice = getIndentPrice(clothObject, indentPrice, costParam, true, type);
				}
				System.out.println("=========2============");
				/** 下面计算goods_special的对应的价格 */
				// 获取安装方式
				String panzhuangfangshi = clothObject.getString("panzhuangfangshi");
				Double high = clothObject.getDouble("high");
				indentPrice = panzhuangfangshi.indexOf("装单") > 0
						? calculateZhuangDan(good, index, high, headShade, indentPrice, true, type)
						:(panzhuangfangshi.indexOf("双联码") > 0?calculateShuangLianMa(good, index, high, headShade, indentPrice, false, type):indentPrice) ;
			}
		}
		return indentPrice;
	}

	/**
	 * 配件的panZhuangfanshi含有双联码的情况下来计算帘头的价格 xiaGuanghui 2017年6月13日 下午1:54:11
	 * 
	 * @param good
	 * @param jsonObject
	 * @param headShade
	 * @param indentPrice
	 * @param b
	 * @return
	 */
	private static Double calculateShuangLianMa(Good good, int index, Double high, String headShade, Double indentPrice,
			boolean b, Integer type) {
		if (null != headShade && !"".equals(headShade)) {
			JSONArray headArray = JSONArray.parseArray(headShade);
			if (index < headArray.size()) {
				JSONObject headObject = headArray.getJSONObject(index);
				// 准备计算帘头价格需要从数据查找的价格
				Map<String, Object> headMap = new HashMap<>();
				headMap.put("type", "帘头");
				headMap.put("cityCode", cityCodeStr);
				headObject.put("high", high);// 计算帘头时没有高度需要在此存放
				indentPrice = getIndentPrice(headObject, indentPrice, headMap, b, type);
				return indentPrice;
			} else {
				return indentPrice;
			}
		} else {
			return indentPrice;
		}
	}

	/**
	 * 配件的panZhuangfanshi含有装单的情况下来计算帘头的价格 xiaGuanghui 2017年6月13日 下午1:45:03
	 * 
	 * @param good
	 * @param clothObject
	 * @param headShade
	 * @param indentPrice
	 * @param b
	 * @return
	 */
	private static Double calculateZhuangDan(Good good, int index, Double high, String headShade, Double indentPrice,
			boolean b, Integer type) {
		if (null != headShade && !"".equals(headShade)) {
			JSONArray headArray = JSONArray.parseArray(headShade);
			if (index < headArray.size()) {
				JSONObject headObject = headArray.getJSONObject(index);
				// 准备计算帘头价格需要从数据查找的价格
				Map<String, Object> headMap = new HashMap<>();
				headMap.put("type", "帘头");
				headMap.put("cityCode", cityCodeStr);
				headObject.put("high", high);
				indentPrice = getIndentPrice(headObject, indentPrice, headMap, b, type);
				return indentPrice;
			} else {
				return indentPrice;
			}
		} else {
			return indentPrice;
		}
	}

	/**
	 * 分配单纱或者布纱双层 xiaGuanghui 2017年6月12日 下午4:41:07
	 * 
	 * @param good
	 * @param clothShade
	 * @param yarnShade
	 * @param headShade
	 * @return
	 */
	private static Double calculateFabricYarnOrClothAndYarn(Double indentPrice, Good good, String clothShade,
			String yarnShade, String headShade, Integer type) {
		int structure = good.getStructure();
		return structure == 1 ? calculateFabricYarn(indentPrice, good, yarnShade, headShade, type)
				: calculateFabricClothAndYarn(indentPrice, good, clothShade, yarnShade, headShade, type);
	}

	/**
	 * 单纱 xiaGuanghui 2017年6月12日 下午4:39:15
	 * 
	 * @param good
	 * @param yarnShade
	 * @param headShade
	 * @return
	 */
	private static Double calculateFabricYarn(Double indentPrice, Good good, String yarnShade, String headShade,
			Integer type) {
		JSONArray yarnArray = JSONArray.parseArray(yarnShade);
		for (int index = 0; index < yarnArray.size(); index++) {
			JSONObject yarnObject = (JSONObject) yarnArray.get(index);
			/**
			 * 计算goods_special的accId对应的goods的价格
			 * 需要goods的accessoriesType的值,goods所属城市 goods的saleNumber,high
			 */
			// 准备查找城市收费的标准的参数
			Map<String, Object> costParam = new HashMap<>();
			costParam.put("type", yarnObject.getString("accessoriesType"));
			costParam.put("cityCode", cityCodeStr);
			indentPrice = getIndentPrice(yarnObject, indentPrice, costParam, true, type);
			/** 下面计算goods_special的对应的价格 */
			// 获取安装方式
			String panzhuangfangshi = yarnObject.getString("panzhuangfangshi");
			Double high = yarnObject.getDouble("high");
			indentPrice = panzhuangfangshi.indexOf("装单") > 0
					? calculateZhuangDan(good, index, high, headShade, indentPrice, true, type)
					: calculateShuangLianMa(good, index, high, headShade, indentPrice, false, type);
		}
		return indentPrice;
	}

	/**
	 * 布纱双层布艺帘 xiaGuanghui 2017年6月12日 下午4:40:32
	 * 
	 * @param good
	 * @param clothShade
	 * @param headShade
	 * @return
	 */
	private static Double calculateFabricClothAndYarn(Double indentPrice, Good good, String clothShade,
			String yarnShade, String headShade, Integer type) {
		int installCase = good.getInstallCase();
		// 如果flag=true,说明布纱双层安装位置一致
		boolean flag = installCase == 1 && (yarnShade == null || "".equals(yarnShade));
		
		
		
		return flag == true ? calculateInstallCase1(indentPrice, good, clothShade, headShade, type)
				: calculateInstallCase0(indentPrice, good, clothShade, yarnShade, headShade, type);
	}

	/**
	 * 布纱安装位置不一致 此时纱有自己的尺寸,不在和布共用一套尺寸 xiaGuanghui 2017年6月13日 下午2:32:15
	 * 
	 * @param indentPrice
	 * @param good
	 * @param clothShade
	 * @param yarnShade
	 * @param headShade
	 * @return
	 */
	private static Double calculateInstallCase0(Double indentPrice, Good good, String clothShade, String yarnShade,
			String headShade, Integer type) {
		// 解析纱的尺寸
		JSONArray yarnArray = JSONArray.parseArray(yarnShade);
		// 遍历所有纱尺寸并且计算纱尺寸的价格
		for (int index = 0; index < yarnArray.size(); index++) {
			JSONObject jsonObject = yarnArray.getJSONObject(index);
			// 查找纱配件价格标准参数准备
			Map<String, Object> yarnMap = new HashMap<>();
			yarnMap.put("type", jsonObject.getString("accessoriesType"));
			yarnMap.put("cityCode", cityCodeStr);
			indentPrice = getIndentPrice(jsonObject, indentPrice, yarnMap, true, type);
		}
		// 解析布的尺寸并计算布的尺寸
		JSONArray clothArray = JSONArray.parseArray(clothShade);
		for (int index = 0; index < clothArray.size(); index++) {
			JSONObject jsonObject = clothArray.getJSONObject(index);
			String panzhuangfangshi = jsonObject.getString("panzhuangfangshi");
			Double high = jsonObject.getDouble("high");
			if (panzhuangfangshi.indexOf("装单") >= 0) {
				// 证明全部单独安装,因为没有纱配件，所有按照
				// 计算布配件安装费
				// 根据配件类型还有地域获得计算方式
				Map<String, Object> bupeijian = new HashMap<>();
				bupeijian.put("type", jsonObject.getString("accessoriesType"));
				bupeijian.put("cityCode", cityCodeStr);
				// 第一次计价，相当于计算单布安装费
				indentPrice = getIndentPrice(jsonObject, indentPrice, bupeijian, true, type);
				// 计算帘头配件安装费
				// 判断是否有帘头
				if (null != headShade && !"".equals(headShade)) {// 帘头按照单层计算(布纱双层分开，顶装单安装码，布纱帘全独立安装)
					// 获得帘头配件
					Map<String, Object> lianpeijian = new HashMap<>();
					lianpeijian.put("type", "帘头");
					lianpeijian.put("cityCode", cityCodeStr);
					indentPrice = calculateZhuangDan(good, index, high, headShade, indentPrice, true, type);
				}
			} else if (panzhuangfangshi.indexOf("装双") >= 0) {
				Map<String, Object> bupeijian = new HashMap<>();
				bupeijian.put("type", jsonObject.getString("accessoriesType"));
				bupeijian.put("cityCode", cityCodeStr);
				indentPrice = getIndentPrice(jsonObject, indentPrice, bupeijian, true, type);
				if (null != headShade && !"".equals(headShade)) {// 帘头按照单层计算(布纱双层分开，顶装单安装码，布纱帘全独立安装)
					// 获得帘头配件
					Map<String, Object> lianpeijian = new HashMap<>();
					lianpeijian.put("type", "帘头");
					lianpeijian.put("cityCode", cityCodeStr);
					indentPrice = calculateZhuangDan(good, index, high, headShade, indentPrice, false, type);
				}

			} else if (panzhuangfangshi.indexOf("双联码") >= 0) {
				if (good.getInstallCase() == 1) {// 布纱安装情况一致 证明没有帘头
					// 计算布配件安装费
					// 根据配件类型还有地域获得计算方式
					Map<String, Object> bupeijian = new HashMap<>();
					bupeijian.put("type", jsonObject.getString("accessoriesType"));
					bupeijian.put("cityCode", cityCodeStr);
					indentPrice = getIndentPrice(jsonObject, indentPrice, bupeijian, false, type);
				} else {// 布纱安装情况不一致 证明有帘头
						// 证明布+纱单独安装 帘头不单独
						// 计算布配件安装费
						// 根据配件类型还有地域获得计算方式
					Map<String, Object> bupeijian = new HashMap<>();
					bupeijian.put("type", jsonObject.getString("accessoriesType"));
					bupeijian.put("cityCode", cityCodeStr);
					indentPrice = getIndentPrice(jsonObject, indentPrice, bupeijian, true, type);
					// 计算帘头配件安装费
					// 判断是否有帘头
					if (null != headShade && !"".equals(headShade)) {// 帘头按照单层计算(布纱双层分开，顶装单安装码，布纱帘全独立安装)
						// 获得帘头配件
						Map<String, Object> lianpeijian = new HashMap<>();
						lianpeijian.put("type", "帘头");
						lianpeijian.put("cityCode", cityCodeStr);
						indentPrice = calculateZhuangDan(good, index, high, headShade, indentPrice, false, type);
					}
				}
			} else {
				Map<String, Object> bupeijian = new HashMap<>();
				bupeijian.put("type", jsonObject.getString("accessoriesType"));
				bupeijian.put("cityCode", cityCodeStr);
				indentPrice = getIndentPrice(jsonObject, indentPrice, bupeijian, false, type);
				// 获得帘头配件
				indentPrice = calculateZhuangDan(good, index, high, headShade, indentPrice, false, type);
			}
		}
		return indentPrice;
	}

	/**
	 * 布纱安装位置一致 xiaGuanghui 2017年6月13日 下午2:31:48
	 * 
	 * @param indentPrice
	 * @param good
	 * @param clothShade
	 * @param headShade
	 * @return
	 */
	private static Double calculateInstallCase1(Double indentPrice, Good good, String clothShade, String headShade,
			Integer type) {
		// 解析布的尺寸并计算布的尺寸
		JSONArray clothArray = JSONArray.parseArray(clothShade);
		// 没有纱尺寸时走的逻辑
		for (int index = 0; index < clothArray.size(); index++) {
			JSONObject jsonObject = clothArray.getJSONObject(index);
			String panzhuangfangshi = jsonObject.getString("panzhuangfangshi");
			Double high = jsonObject.getDouble("high");
			// 判断其中包含的字符串
			if (panzhuangfangshi.indexOf("装单") >= 0) {//
				// 证明全部单独安装,因为没有纱配件，所有按照
				// 计算布配件安装费
				// 根据配件类型还有地域获得计算方式
				Map<String, Object> bupeijian = new HashMap<>();
				bupeijian.put("type", jsonObject.getString("accessoriesType"));
				bupeijian.put("cityCode", cityCodeStr);
				indentPrice = getIndentPrice(jsonObject, indentPrice, bupeijian, true, type);
				indentPrice = getIndentPrice(jsonObject, indentPrice, bupeijian, true, type);
				// 计算帘头配件安装费
				// 判断是否有帘头
				if (null != headShade && !"".equals(headShade)) {// 帘头按照单层计算(布纱双层分开，顶装单安装码，布纱帘全独立安装)
					indentPrice = calculateZhuangDan(good, index, high, headShade, indentPrice, true, type);
				}
			} else if (panzhuangfangshi.indexOf("装双") >= 0) {
				// 证明布纱双层安装
				// 计算布配件安装费
				// 根据配件类型还有地域获得计算方式
				Map<String, Object> bupeijian = new HashMap<>();
				bupeijian.put("type", jsonObject.getString("accessoriesType"));
				bupeijian.put("cityCode", cityCodeStr);
				indentPrice = getIndentPrice(jsonObject, indentPrice, bupeijian, false, type);
				// 计算帘头配件安装费
				// 判断是否有帘头
				if (null != headShade && !"".equals(headShade)) {// 帘头按照单层计算(布纱双层分开，顶装单安装码，布纱帘全独立安装)
					indentPrice = calculateZhuangDan(good, index, high, headShade, indentPrice, true, type);
				}
			} else if (panzhuangfangshi.indexOf("双联码") >= 0) {
				// 判断布纱安装位置是否一致
				if (good.getInstallCase() == 1) {// 布纱安装情况一致 证明没有帘头
					// 计算布配件安装费
					// 根据配件类型还有地域获得计算方式
					Map<String, Object> bupeijian = new HashMap<>();
					bupeijian.put("type", jsonObject.getString("accessoriesType"));
					bupeijian.put("cityCode", cityCodeStr);
					indentPrice = getIndentPrice(jsonObject, indentPrice, bupeijian, false, type);
				} else {// 布纱安装情况不一致 证明有帘头
						// 证明布+纱单独安装 帘头不单独
						// 计算布配件安装费
						// 根据配件类型还有地域获得计算方式
					Map<String, Object> bupeijian = new HashMap<>();
					bupeijian.put("type", jsonObject.getString("accessoriesType"));
					bupeijian.put("cityCode", cityCodeStr);
					indentPrice = getIndentPrice(jsonObject, indentPrice, bupeijian, true, type);
					// 计算帘头配件安装费
					// 判断是否有帘头
					if (null != headShade && !"".equals(headShade)) {// 帘头按照单层计算(布纱双层分开，顶装单安装码，布纱帘全独立安装)

						indentPrice = calculateZhuangDan(good, index, high, headShade, indentPrice, false, type);
					}
				}
			} else {// 三联码 这种情况有吗？
					// 证明布+纱双层安装 帘头不单独
					// 计算布配件安装费
					// 根据配件类型还有地域获得计算方式
				Map<String, Object> bupeijian = new HashMap<>();
				bupeijian.put("type", jsonObject.getString("accessoriesType"));
				bupeijian.put("cityCode", cityCodeStr);
				indentPrice = getIndentPrice(jsonObject, indentPrice, bupeijian, false, type);
				indentPrice = calculateZhuangDan(good, index, high, headShade, indentPrice, false, type);
			}
		}
		return indentPrice;
	}

	/**
	 * 计算价格程序 xiaGuanghui 2017年6月12日 下午5:39:06
	 * 
	 * @param clothObject
	 * @param indentPrice
	 * @param string
	 * @return
	 */
	private static Double getIndentPrice(JSONObject jsonObject, Double indentPrice, Map<String, Object> costParam,
			boolean singleFlag, Integer type) {
		if(jsonObject==null || "".equals(jsonObject.getString("saleNumber")) ||  "".equals(jsonObject.getString("high"))){
			return indentPrice;
		}
		// 获取该商品对应的城市收费标准
		costParam.put("territoryType", type);
		CityCostRule cityCostRule = cityCostRuleDAO.selectAPrice(costParam);
		if(cityCostRule==null){
			return indentPrice;
		}
		/** 判断配件宽的米数与基础宽米数(宽度4米内包括4米为基础米数)(如果配件宽超出4米数,超出部分需要额外计算) */
		Double widthBasicMeter =null;
		Double heightBasicMeter =null;
		
		// 基础宽米数的值
		 widthBasicMeter = cityCostRule.getWidthBasisMeter();
		// 基础高米数的值
		 heightBasicMeter = cityCostRule.getHeightBasisMeter();
		// 配件的宽米数
		double width = jsonObject.getDoubleValue("saleNumber");
		/** 判断配件高的米数与基础高米数(高度3米内包括3米为基础米数)(如果配件高超出3米数,超出部分需要额外计算) */
		// 配件的高米数
		double height = jsonObject.getDoubleValue("high");
		/** 通过比较高米数和宽米数分为以下四种情况 */
		// 1宽和高都没有超标
		if (width <= widthBasicMeter && height <= heightBasicMeter) {
			// 直接计算需要计算的价格
			indentPrice = Arith.add(indentPrice, singleFlag ? cityCostRule.getSingle() : cityCostRule.getDoubleDeck());

			// 2宽超标,高没有超标
		} else if (width > widthBasicMeter && height <= heightBasicMeter) {
			indentPrice = calculateWidthOver(cityCostRule, jsonObject, width, widthBasicMeter, indentPrice, singleFlag);

			// 3宽没有超标,高超标
		} else if (width <= widthBasicMeter && height > heightBasicMeter) {
			indentPrice = calculateHighOver(cityCostRule, jsonObject, height, heightBasicMeter, indentPrice,
					singleFlag);

			// 4宽和高都没有超标
		} else if (width > widthBasicMeter && height > heightBasicMeter) {
			indentPrice = calculateWidthAndHeightOver(cityCostRule, jsonObject, width, widthBasicMeter, height,
					heightBasicMeter, indentPrice, singleFlag);

			// 错误情况
		} else {
			logger.debug("没有改情况");
		}

		return indentPrice;
	}

	/**
	 * 计算高和宽都超出的价格 xiaGuanghui 2017年6月13日 上午11:23:29
	 * 
	 * @param cityCostRule
	 * @param jsonObject
	 * @param width
	 * @param widthBasicMeter
	 * @param height
	 * @param heightBasicMeter
	 * @param indentPrice
	 * @param singleFlag
	 * @return
	 */
	private static Double calculateWidthAndHeightOver(CityCostRule cityCostRule, JSONObject jsonObject, double width,
			double widthBasicMeter, double height, double heightBasicMeter, Double indentPrice, boolean singleFlag) {
		// 计算商家价格
		double basePrice = singleFlag ? cityCostRule.getSingle() : cityCostRule.getDoubleDeck();
		// 高度超标量
		Double reductionHeight = Arith.sub(height, heightBasicMeter);
		// 宽度超标量
		Double reductionWidth = Arith.sub(width, widthBasicMeter);

		Double chaochus = Arith.mul((Math.ceil(Arith.div(reductionHeight, cityCostRule.getHeightBeyondMeter()))),
				cityCostRule.getHeightBeyondUnitPrice());
		Double heightPrice = Arith.add(basePrice, chaochus);
		Double total = Arith.mul((Math.ceil(Arith.div(reductionWidth, cityCostRule.getWidthBeyondMeter()))) + 1,
				heightPrice);
		indentPrice = Arith.add(total, indentPrice);

		return indentPrice;
	}

	/**
	 * 计算高超出价格 xiaGuanghui 2017年6月13日 上午11:08:27
	 * 
	 * @param cityCostRule
	 * @param jsonObject
	 * @param height
	 * @param heightBasicMeter
	 * @param indentPrice
	 * @param singleFlag
	 * @return
	 */
	private static Double calculateHighOver(CityCostRule cityCostRule, JSONObject jsonObject, double height,
			double heightBasicMeter, Double indentPrice, boolean singleFlag) {
		// 获取基础价格
		double basePrice = singleFlag ? cityCostRule.getSingle() : cityCostRule.getDoubleDeck();
		// 高度超标量
		Double reduction = Arith.sub(height, heightBasicMeter);
		// 计算高超出的价格
		Double overHightPrice = Arith.mul((Math.ceil(Arith.div(reduction, cityCostRule.getHeightBeyondMeter()))),
				cityCostRule.getHeightBeyondUnitPrice());
		// 计算需要计算的价格
		indentPrice = Arith.add(Arith.add(basePrice, overHightPrice), indentPrice);

		return indentPrice;
	}

	/**
	 * 计算宽超标的价格 xiaGuanghui 2017年6月13日 上午10:35:07
	 * 
	 * @param cityCostRule
	 * @param jsonObject
	 * @param indentPrice
	 * @param singleFlag
	 * @return
	 */
	private static Double calculateWidthOver(CityCostRule cityCostRule, JSONObject jsonObject, double width,
			double widthBasicMeter, Double indentPrice, boolean singleFlag) {
		// 获取基础价格
		double basePrice = singleFlag ? cityCostRule.getSingle() : cityCostRule.getDoubleDeck();
		// 计算宽超出的部分米数
		Double reduction = Arith.sub(width, widthBasicMeter);
		// 计算超出价格
		Double overWidthPrice = Arith.mul(Math.ceil(Arith.div(reduction, cityCostRule.getWidthBeyondMeter())),
				basePrice);
		// 计算需要的价格
		indentPrice = Arith.add(Arith.add(basePrice, overWidthPrice), indentPrice);
		return indentPrice;
	}

}
